//
// Created by ins on 2021/2/28.
//

#include "NpcDao.h"
#include "NpcBean.h"
#include "TaskDao.h"
#include "TaskBean.h"
#include <kisql.h>
#include "tool/Util.h"
#include <iostream>
#include <string>
#include <list>

using std::string;
using std::list;
static kisql *db;
static MYSQL_RES *result;
static MYSQL_ROW row;

NpcDao *NpcDao::sInstance = new NpcDao();
NpcDao::NpcDao() {
    db = kisql::getInstance();
}
NpcDao::~NpcDao() {}
NpcDao* NpcDao::getInstance() {return sInstance;}

/* 将 task id 的json 数组， 解析后， 再查找， 组成 id, name 两个字段 */
static string recombine_task_json_data(string task_json_str)
{
    Json::Value task_root = Util::init_root(task_json_str);

    string strOut = "[";
    for(int i = 0; i < task_root.size(); i++){
        
        TaskBean taskBean;
        TaskDao *taskDao = TaskDao::getInstance();

        Json::Value taskNode = task_root[i];
        string taskid = taskNode["id"].asString();

        taskDao->getTask(taskid, taskBean);
        strOut += "{";
        strOut += "\"id\":" + taskid;
        strOut += ",\"name\":\"" + taskBean.getName() +"\"";
        strOut += ",\"lx\":" + taskBean.getLx();
        strOut += "},";
    }
    int pos = strOut.rfind(",");
    strOut.replace(pos,1,"]");

    return strOut;
}

void NpcDao::getNpc(int id, NpcBean &retNpc) {

    char sql[1024] = {0};
    sprintf(sql, R"(select * from npc where id="%d")",
            id);

    if(mysql_query(db->getMysql(), sql)){
        perror("mysql query err");
        return;
    } else{ //select success
        result = mysql_store_result(db->getMysql());
        if(result){
            int num_rows = mysql_num_rows(result);

            for(int i = 0; i < num_rows; i++) {
                row = mysql_fetch_row(result);
                if (row == nullptr) {
                    break;
                }

                retNpc.setId(row[0]);
                retNpc.setName(row[3]);
                retNpc.setDesc(row[4]);
                retNpc.setGender(row[5]);
                retNpc.setAge(row[6]);
                retNpc.setExp(row[7]);
                retNpc.setTaskId(row[8]);
                retNpc.setGreeting(row[10]);         //make task id

                if(retNpc.getTaskId() != "0"){
                    string strOut;
                    strOut = recombine_task_json_data(retNpc.getTaskId());
                    retNpc.setTaskId(strOut);
                }

                //cout << "retNpc: " << retNpc.toString() << endl;
            }
        }
    }
}

void NpcDao::getNpcMoreInfo(int id, NpcMoreInfoBean &retNpc) {

    char sql[1024] = {0};
    sprintf(sql, R"(select * from npc where id="%d")", //TODO
            id);

    if(mysql_query(db->getMysql(), sql)){
        perror("mysql query err");
        return;
    } else{ //select success
        result = mysql_store_result(db->getMysql());
        if(result){
            int num_rows = mysql_num_rows(result);

            for(int i = 0; i < num_rows; i++) {
                row = mysql_fetch_row(result);
                if (row == nullptr) {
                    break;
                }

                //TODO
                retNpc.setId(row[0]);
                cout << "retNpc: " << retNpc.toString() << endl;
            }

        }
    }
}
